<template>
   <div class="login-wrap" >
    <el-form
    class="login-form"
    label-position="top"
    label-width="80px"
     :model="formdata">
     <h2>用户登录</h2>
  <el-form-item label="用户名">
    <el-input v-model="formdata.username"></el-input>
  </el-form-item>
  <el-form-item label="密码">
    <el-input v-model="formdata.password" type="password"></el-input>
  </el-form-item>
   <el-button type="primary" class="login-btn" :plain="true" @click.prevent="clickLogin">登录</el-button>
</el-form>
   </div>
</template>
<script>
export default {
  data () {
    return {
      formdata: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    clickLogin () {
      // 不同用户登录，通过返回不同的token ，判断具有哪些功能 从而返回不用的meun
      this.$http.post('login', this.formdata).then(res => {
        // console.log(res.data)
        const { data, meta: { msg, status } } = res.data
        if (status === 200) {
          localStorage.setItem('token', data.token)
          //  console.log(localStorage.getItem('token'))
          this.$message.success(msg)
          this.$router.push({
            name: 'home'
          })
          //  this.$router.push('/home')
        } else if (msg === '参数错误') {
          this.$message.error('用户名或密码不能为空')
        } else {
          this.$message.error(msg)
        }
      })
    }
  }
}
</script>
<style lang="scss" >
.login-wrap{
     height: 100%;
     background: #324152;
     display: flex;
     justify-content:center;
     align-items: center;
}
.login-wrap .login-form{
    background: #fff;
    border-radius: 5px;
    padding: 30px;
    width: 400px;
    .login-btn{
        width: 100%;
    }
}
</style>
